<?php
/**
* 2018年1月18日 11:17:09
* 收钱吧接口对接
* author:lzw
* 
* 使用流程
* 1.疯狂收钱吧注册推广商户，填写商户入网资料
* 2.在疯狂收钱吧获取激活码 放入下方私有变量code中
* 3.调用下方activate方法  生成terminal_sn 和 terminal_key变量并放入下方对应私有变量中
* 4.调用相应方法实现不同的支付场景  wxPay(微信公众号支付)  wapPay(APP支付)  precreate(C扫B)
*/
class Shouqianba
{
	
	private $vendor_sn = '91800308';

	private $vendor_key = '8f7ba5199321c833f52279931b83987d';

    private $appid = '2018011800000527';

    private $host = 'https://api.shouqianba.com';

    private $code = '18293489';			//激活码

    private $terminal_sn = '100005270005711313';	//终端序列号

    private $terminal_key = 'bae78f9467d6ad5cb81050bfe9d9bcdd';		//终端密钥

	/**
	 * [微信支付方法]
	 * @Author   lzw
	 * @DateTime 2018-02-02
	 * @return   [type]     [description]
	 */
	function wxPay($data)
	{	
		// 字段顺序不能变
		$post_data = array(
			'client_sn'			=>		$this->getOrderId(),	//订单编号
			'notify_url'		=>		$data['notify_url'],	//回调地址
			'operator'			=>		'lzw',					//门店操作员名称
			'return_url'		=>		$data['return_url'],	//支付完成同步跳转地址
			'subject'			=>		$data['subject'],		//商品名称
			'terminal_sn'		=>		$this->terminal_sn,	
			'total_amount'		=>		$data['total_amount'],	//价格  以分为单位
			);

		$query = "";

		foreach ($post_data as $key => $value)
		{
			$query .= $key."=".$value."&";
		}

		$sign = strtoupper(md5($query.'key='.$this->terminal_key));

    	$query = $query."sign=".$sign;

    	header("Location:https://qr.shouqianba.com/gateway?".$query);
	}

	/**
	 * [短信充值预下单接口]
	 * @Author   zt
	 * @DateTime 2018-04-04
	 * @param    [type]     $type         [支付方式]
	 * @param    [type]     $order_number [订单号]
	 * @param    [type]     $total_amount [订单金额]
	 * @param    [type]     $goods_name   [商品名称]
	 * @return   [type]                   [description]
	 */
	function message_pay($type, $order_number, $total_amount, $goods_name, $notify_url, $return_url)
	{
		$url = $this->host.'/upay/v2/precreate';

		//请求数据
		$post_data = array(
			'terminal_sn'		=>		$this->terminal_sn,
			'client_sn'			=>		$order_number,
			'total_amount'		=>		'1',
			'payway'			=>		$type.'',
			'subject'			=>		$goods_name,
			'operator'			=>		'lzw',
			'notify_url'		=>		$notify_url,
			'return_url'		=>		$return_url,
			);	

		//设置请求头
		$header = array('Content-Type: application/json',$this->signHeader($post_data,1));

		$result = $this->_post($url,json_encode($post_data),$header);
		
		if ($result['result_code'] == '200')
		{	
			return array('code'=>1,'msg'=>'操作成功','code_url'=>$result['biz_response']['data']['qr_code_image_url']);
		}
		else
		{
			return array('code'=>0,'msg'=>'操作失败');
		}
	}

	/**
	 * http post请求
	 * @param  [type]  $url    [description]
	 * @param  [type]  $data   [description]
	 * @param  integer $header [description]
	 * @return [type]          [description]
	 */
	function _post($url, $data, $header = 0){
		//初始化curl会话
		$curl = curl_init();
		//设置请求地址
		curl_setopt($curl, CURLOPT_URL, $url);
		//设置返回
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//设置post请求
		curl_setopt($curl, CURLOPT_POST, 1);
		//设置post数据
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		//设置代理 本地不设置会报错
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		//设置http请求头
		if(is_array($header)){
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		}
		//运行curl
		$result = curl_exec($curl);
		//资源关闭
		curl_close($curl);

		return json_decode($result,true);

	}

	/**
	 * [激活接口]
	 * @Author   lzw
	 * @DateTime 2018-01-18
	 * @return   [type]     [description]
	 */
	function activate()
	{
		$url = $this->host.'/terminal/activate';

		//请求数组
		$post_data =  array(
			'app_id'		=>		$this->appid,
			'code'			=>		$this->code,
			'device_id'		=>		$this->get_device_id()		
			);

		//设置请求头
		$header = array('Content-Type: application/json',$this->signHeader($post_data));

		$result = $this->_post($url,json_encode($post_data),$header);

		echo '<pre>';var_dump($result);exit;
	}	

	/**
	 * [获取唯一设备号方法]
	 * @Author   lzw
	 * @DateTime 2018-01-18
	 * @return   [type]     [description]
	 */
	function get_device_id()
	{
		return date('YmdHis').rand(1000,9999);
	}

	/**
	 * [签名算法]
	 * @Author   lzw
	 * @DateTime 2018-01-18
	 * @return   [type]     [description]
	 */
	function signHeader($data,$type = 0)
	{	
		if ($type == 0)
		{	
			//激活接口用vendor参数签名

			$sign = md5(json_encode($data).$this->vendor_key);

			return "Authorization: ".$this->vendor_sn." ".$sign;
		}
		else
		{	
			//其余接口用terminal参数签名

			$sign = md5(json_encode($data).$this->terminal_key);

			return "Authorization: ".$this->terminal_sn." ".$sign;
		}
	}

	/**
	 * [生成订单号]
	 * @Author   lzw
	 * @DateTime 2017-12-14
	 * @return   [type]     [description]
	 */
	function getOrderId()
	{
        $orderid = date('Ymd') . str_pad(mt_rand(1, 99999), 7, '0', STR_PAD_LEFT);
        return $orderid;
	}
}